<?php
//测试控制器类
class CommonController extends Controller{
	protected $model;
	public function __init(){
		
			$this->model = K('User');
		}


	public function login(){
			if(IS_POST){
				
			if(strtoupper($_POST['code'])!=$_SESSION['code']) $this->error( "验证码错误");
			$username = Q('post.username');
			$password = Q('post.password',null,"md5");
			
			$userInfo = $this->model->get_one_data(array('username'=>$username));

			if(empty($userInfo)) $this->error("用户名不存在");
			if($password != $userInfo['passwd']) $this->error("密码错误");
			$_SESSION['uid'] = $userInfo['uid'];
			$_SESSION['username'] = $userInfo['username'];

			$this->success('登入成功',U('index'));



			}
			
		}


	public function code(){
			$code = new Code();
			$code->show();
		}

		// AJAX 验证验证码
		public function ajax_code(){
			if(IS_AJAX){
				if(strtoupper($_POST['code'])==$_SESSION['code']){// 验证通过
				echo 1;
				}else{// 验证失败
				 echo 0;
				}
				exit;
			}
		}
		// 验证用户名是否存在
		public function ajax_user(){
			if(IS_AJAX){
				$username = Q("post.username");
				$userInfo = $this->model->get_data(array('username'=>$username));
				if($userInfo){
					echo 1;
				}else{
				echo 0;	
				}
				exit;
			}
		}

		public function quit(){
  		session(null);
  		$this->success('退出成功',U('index'));
  		}
   
}
